<?php
require('globals.php');

$dealer_id = $_GET['id'];

$grabber_query = "SELECT * from " . $dealers_table . " WHERE dealer_id=" . $dealer_id;
$grabber_query_b = "SELECT * from " . $tables_table . " WHERE row_a_assignment =" . $dealer_id . " OR row_b_assignment =" . $dealer_id;

if (!($link = mysql_connect($host, $user, $password)))
	die('Could not connect: ' . mysql_error());

if (!(mysql_select_db($database)))
	die('Could not select db ' . $database . ': ' . mysql_error());

$result = mysql_query($grabber_query, $link);
$table_result = mysql_query($grabber_query_b, $link);

if (!$result || !table_result)
	die('Either no matches or the query failed...' . mysql_error());

$range = mysql_num_fields($result);
$range_b = mysql_num_fields($table_result);
$values = mysql_fetch_row($result);
if (mysql_num_rows($table_result) == 2) {		// quick hack to make notifications work (doubles)
	$a = mysql_fetch_row($table_result);
	$b = mysql_fetch_row($table_result);
	$tableinfo = $a[0] . "/" . $b[0];
}
if (mysql_num_rows($table_result) == 3) {		// quick hack to make notifications work (triples)
	$a = mysql_fetch_row($table_result);
	$b = mysql_fetch_row($table_result);
	$c = mysql_fetch_row($table_result);
	$tableinfo = $a[0] . "/" . $b[0] . "/" . $c[0];
}
if (mysql_num_rows($table_result) == 1 || mysql_num_rows($table_result) == 0) {
	$a = mysql_fetch_row($table_result);
	$tableinfo = $a[0];
	$table_assignments[0] = $a[1];
	$table_assignments[1] = $a[2];
}

$escaped_dealer = htmlspecialchars($values[3]);

$counter=0;
?>
<html>
<head>

<SCRIPT type="text/javascript" src="prototype.js"></SCRIPT>
<SCRIPT type="text/javascript" src="dealers.php"></SCRIPT>
<SCRIPT type="text/javascript">

function defaultValuation() {

	// This is an ugly-butt function that tries really hard
	// to set the editor up with the default values as culled
	// from the database call.

	document.submit.table_size_req.value = <?=$values[4]?>;
	document.submit.payment_method.value = <?=$values[23]?>;
	document.submit.table_update.value = 0;

	var tablesize = <?=$values[4]?>;
	var power_req = <?=$values[11] ?>;
	var wall_req = <?=$values[6] ?>;
	var tablename = "<?=$tableinfo ?>";
	var a_assignment = "<?=$table_assignments[0]?>";
	var b_assignment = "<?=$table_assignments[1]?>";
	var dealer_id = "<?=$dealer_id ?>";

	//rewritten to compensate for new table handling; the Full switch may not be necessary
	//anymore, but halves must be handled differently.

	switch(tablesize) {
		case(1):	// full
			break;
		case(2):	// double
			if (Number(tablename.substr(1,2) < 9))
				internal = '0' + String(Number(tablename.substr(1,2))+1);
			else
				internal = String(Number(tablename.substr(1,2))+1);
			tablename = tablename.substr(0,3) + "/" + tablename.substr(0,1) + internal;
			break;
		case(3):	// triple
			if (Number(tablename.substr(1,2) == 8)) {
				internal_a = '0' + String(Number(tablename.substr(1,2))+1);
				internal_b = String(Number(tablename.substr(1,2))+1);
			}
			else if (Number(tablename.substr(1,2) < 8)) {
				internal_a = '0' + String(Number(tablename.substr(1,2))+1);
				internal_b = '0' + String(Number(tablename.substr(1,2))+1);
			}
			else {
				internal_a = String(Number(tablename.substr(1,2))+1);
				internal_b = String(Number(tablename.substr(1,2))+2);
			}
			tablename = tablename.substr(0,3) + "/"
						+ tablename.substr(0,1) + internal_a + "/"
						+ tablename.substr(0,1) + internal_b;
			break;
		case(0):	// half
			if (a_assignment == dealer_id)
				tablename = tablename + 'A';
			else if (b_assignment == dealer_id)
				tablename = tablename + 'B';
			break;
		default:  // island
			tablename = tablename;
	}
	populateMenus(document.submit.assignment, tablesize, table_array[document.submit.table_size_req.value],
					tablename, power_req, wall_req);
}

function local_toggle() {
	document.submit.update_table.checked ? document.submit.assignment.disabled=false : document.submit.assignment.disabled=true;
}

</SCRIPT>

</head>
<body onload="SetItOff()">


<form id="submit" method="POST" action="http://<?=$LOCAL_IP ?>/Update Dealer.php" name="submit">
<table border=1>
<tr><td align=left>
		<input type=button value="Previous Dealer" onclick="location.href = 'Edit Dealer.php?id=<?=($values[0]-1)?>';"></td>
		<td align=center colspan=2>Dealer ID: <input size=3 READONLY name="dealer_id" value=<?=$values[$counter++]?>></td>
		<td align=right><input type=button value="Next Dealer" onclick="location.href = 'Edit Dealer.php?id=<?=($values[0]+1)?>';"></td>
</td></tr>

<tr><td>Reg. Order ID:</td><td><input size="12" maxlength="12" name="order_id" value="<?=$values[$counter++]?>"></td>
    <td>Date Entered:</td><td><input size="12" maxlength="12" name="date_entered" readonly value="<?=$values[$counter++]?>"></td></tr>

<tr><td>Dealership Name:</td><td colspan=3><input size="60" maxlength="60" name="dealership_name" value="<?=$escaped_dealer; ?>"></td></tr><?php $counter++; ?>

<tr><td>Table Size (Requested):</td><td colspan=3>
  <select size="1" name="table_size_req" onchange="populateMenus(this.form.assignment, this.selectedIndex, table_array[document.submit.table_size_req.value], ((this.selectedIndex == -1) ? null : table_array[this.selectedIndex-1]));">
  <option value=0>Half</option>
  <option value=1>Full</option>
  <option value=2>Double</option>
  <option value=3>Triple</option>
  <option value=4>Island</option>
  </select></td></tr>

<?php $counter++; ?><tr><td>Table Assignment:</td>
<td colspan=1><select size="1" disabled name="assignment">
</select></td><td colspan=2>Update Table? <input name="update_table" UNCHECKED type="checkbox" onclick="local_toggle(update_table,assignment)" value="1">
</td></tr>

<tr><td>Special Requests:</td>

<td colspan=3><input name="table_pref" <?php if ($values[$counter++]) echo("checked=\"true\"")?> value="1" type="checkbox">Specific Table Assignment</td></tr>
<tr><td></td><td colspan=3><input name="wall" value="1" type="checkbox" <?php if ($values[$counter++]) echo("checked=\"true\"")?><td>Wall Space</td></tr>
<?php $counter++; ?> <!-- this filters out the #7 value, which has no value e.e -->
<tr><td></td><td colspan=3><input name="table_adjacent_to" <?php if ($values[$counter++]) echo("checked=\"true\"")?> value="1" type="checkbox">Adjacent To Specific Dealer(s)</td></tr>
<tr><td></td><td colspan=3><input name="table_away_from" <?php if ($values[$counter++]) echo("checked=\"true\"")?> value="1" type="checkbox">Away From Specific Dealer(s)</td></tr>
<tr><td></td><td colspan=3><input name="table_no_request" <?php if ($values[$counter++]) echo("checked=\"true\"")?> value="1" type="checkbox">No Special Requests</td></tr>
<tr><td></td><td colspan=3><input name="power" <?php if ($values[$counter++]) echo("checked=\"true\"")?> value="1" type="checkbox">Active Power</td></tr>

<tr><td>First Name:</td><td colspan=3><input maxlength="30" size="30" name="dealer_first_name" value="<?=$values[$counter++]?>"></td></tr>
<tr><td>Last Name:</td><td colspan=3><input size="30" maxlength="30" name="dealer_last_name" value="<?=$values[$counter++]?>"></td></tr>
<tr><td>Street Address:</td><td colspan=3><input maxlength="45" size="45" name="dealer_address" value="<?=$values[$counter++]?>"></td></tr>
<tr><td>City:</td><td colspan=3><input maxlength="20" size="20" name="dealer_city" value="<?=$values[$counter++]?>"></td></tr>
<tr><td>State/Province/Other:</td><td colspan=3><input maxlength="12" size="12" name="dealer_state" value="<?=$values[$counter++]?>"></td></tr>
<tr><td>Postal Code:</td><td colspan=3><input maxlength="10" size="10" name="dealer_post_code" value="<?=$values[$counter++]?>"></td></tr>
<tr><td>Country:</td><td colspan=3><input maxlength="10" size="10" name="dealer_country" value="<?=$values[$counter++]?>"></td></tr>
<tr><td>Email Address:</td><td colspan=3><input maxlength="35" size="35" name="dealer_email" value="<?=$values[$counter++]?>"></td></tr>
<tr><td>Phone Number:</td><td colspan=3><input maxlength="18" size="18" name="dealer_phone" value="<?=$values[$counter++]?>"></td></tr>
<tr><td>Badge Number:</td><td colspan=3><input maxlength="25" size="25" name="dealer_badge_number" value="<?=$values[$counter++]?>"></td></tr>

<tr><td>Tax Identification Number:</td><td><input maxlength="10" size="10" name="tax_id" value="<?=$values[$counter++]?>"></td></tr>

<?php $counter++; ?><td>Payment Method:</td>
  <td><select size="1" name="payment_method">
  <option selected="selected" value="0">No Payment</option>
  <option value="1">Credit Card</option>
  <option value="2">Check</option>
  <option value="3">Money Order</option>
  <option value="4">Cash</option>
  </select></td>
<td>Total Charge:</td><td><input size="6" readonly name="payment_amount" value="<?=$values[$counter++]?>"></td></tr>

<tr><td>Dealer Notified?</td><td><input disabled name="is_notified" value="1" type="checkbox" <?php if ($values[$counter++]) echo("checked=\"true\"") ?>></td>
<?php //$counter++; ?><td>Notification Date:</td><td><input readonly name="notification_date" size="12" value="<?=$values[$counter++] ?>"></td></tr>

<tr><td>Dealer Cancelled?</td><td><input name="cancelled" value="1" type="checkbox" <?php if ($values[$counter++]) echo("checked=\"true\"") ?>></td>
<td>Cancellation Date:</td><td><input name="cancelled_date" value="<?=$values[$counter++] ?>" size="12"></td></tr>

<tr><td>Issue With Application?</td><td colspan=3><input name="trouble" value="1" <?php if ($values[$counter++]) echo("checked=\"true\"") ?>type="checkbox"></td></tr>
<tr><td colspan=1>Notes:</td><td colspan=3>
<textarea cols="65" rows="4" name="table_note"><?=$values[$counter++]?></textarea></td></tr>

<tr><td colspan=4><center><input type=button value="Notify Dealer"<?php
$escaped_dealer = addslashes($values[3]);
if (!$tableinfo || $values[27])
	echo("disabled>");
else
	echo(" onclick=\"location.href='Mailer.php?dealership=$escaped_dealer&dealername=$values[13],+$values[12]&\
		paymentmethod=$values[23]&taxid=$values[24]&tablesize=$values[4]&\
		email=$values[19]&power=$values[11]&assignment=$tableinfo&type=notification';\">");
?>
  <input type="hidden" name="table_update" id="table_update">
  <input type="submit" value="Submit Changes">
  <input type="reset" value="Reset Form">
  <input type=button value="Back to List" onclick="location.href = 'Dealer List.php';"></td></tr>
</center></table>
</form>
</body>
<script>
	var form = $('submit');
	var element = form.serialize(true);
	//window.alert(element['dealership_name']);
</script>

</html>